﻿using ScreenMonitor.Common;
using ScreenMonitor.Common.File;
using ScreenMonitor.Common.Objects;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ScreenMonitor.ClientLib
{
    /// <summary>
    /// 表示客户端连接的服务端对象
    /// </summary>
    public static class ServerObject
    {

        #region========公有委托========

        public delegate void OperateObjectEventHandler(object sender, OperateObjectEventArgs e);

        #endregion

        #region========公有函数========

        /// <summary>
        /// 表示与服务端对象关联的TcpClient
        /// </summary>
        public static TcpClient ClientBase { get;internal set; }

        /// <summary>
        /// 向服务端发送客户端名称
        /// </summary>
        public static void SetClientName(string Name)
        {
            CommandObjects.TcpCommandCenter.SendCommandWithData(CommandType.SendName, Encoding.UTF8.GetBytes(Name),ClientBase);
        }

        /// <summary>
        /// 向服务端发送RePing指令
        /// </summary>
        public static void Reping()
        {
            CommandObjects.TcpCommandCenter.SendCommand(CommandType.RePing,ClientBase);
        }

        /// <summary>
        /// 向服务端发送文件命令请求
        /// </summary>
        /// <param name="ReqObj"></param>
        public static void SendFile(FileRequestObject ReqObj)
        {
            CommandObjects.TcpCommandCenter.SendCommandWithData(CommandType.SendFile, ReqObj.ToByte(),ClientBase);
        }

        /// <summary>
        /// 向服务端发送图片
        /// </summary>
        /// <param name="ImgData">存储发送图片的字节数组</param>
        public static void SendBitmap(byte[] ImgData)
        {
            CommandObjects.TcpCommandCenter.SendCommandWithData(CommandType.SendBitMap, ImgData, ClientBase);
        }

        /// <summary>
        /// 向服务端发送连接请求
        /// </summary>
        public static void Connect()
        {
            CommandObjects.TcpCommandCenter.SendCommand(CommandType.Connect, ClientBase);
        }

        /// <summary>
        /// 向服务端发送签到完成请求
        /// </summary>
        public static void SignCompelete(SignResponceObject Obj)
        {
            CommandObjects.TcpCommandCenter.SendCommandWithData(CommandType.SignCompelete, Obj.ToByte(),ClientBase);
        }

        #endregion

        #region==========事件==========

        /// <summary>
        /// 当服务端发送消息时执行的事件
        /// </summary>
        public static event OperateObjectEventHandler SendMessage;

        /// <summary>
        /// 当服务端要求签到时执行的事件
        /// </summary>
        public static event OperateObjectEventHandler NeedToSign;

        /// <summary>
        /// 当服务端请求文件发送时执行的事件
        /// </summary>
        public static event OperateObjectEventHandler WantToSendFile;


        #endregion

        #region======事件公开函数======

        public static void OnWantToSendFile(object sender,OperateObjectEventArgs e)
        {
            if(WantToSendFile!=null)
            {
                WantToSendFile(sender, e);
            }
        }

        public static void OnSendMessage(object sender,OperateObjectEventArgs e)
        {
            if (SendMessage != null)
                SendMessage(sender, e);
        }

        public static void OnNeedToSign(object sender,OperateObjectEventArgs e)
        {
            if(NeedToSign!=null)
                NeedToSign(sender, e);
        }


        #endregion

    }
}
